<template>
  <main class="setting-page">
    <div class="form-title">
      <p class="title">扩展协议</p>
      <a-button class="common-btn" preIcon="ant-design:plus-outlined" @click="handleAdd">新增</a-button>
    </div>
    <section class="card-list">
      <section class="card" v-for="(item, index) in protocolList" :key="index">
        <div class="card-head">
          <div class="left">
            <img class="card-image" :src="p.img" v-for="(p, idx) in extensions" :key="idx" v-show="item.protocolType == p.protocolType" />
            <p class="card-name"> {{ item.protocolType }} </p>
          </div>
          <div class="right">
            <img
              src=""
              class="status-img"
              v-show="item.status"
            />
            <img
              src=""
              class="status-img"
              v-show="!item.status"
            />
          </div>
        </div>
        <div class="card-bottom">
          <p class="card-row"> 端口： {{ item.port }} </p>
          <div class="btns">
            <img
              class="row-btn-img"
              :src="i.img"
              v-for="(i, idx) in actionBtns"
              :key="idx"
              @click="headerBtnItemHandle(item, i.click)"
              v-show="i.click.defaultshow || i.click.isshow == item.enable"
            />
          </div>
        </div>
      </section>
    </section>
    <ProtocolModal @register="registerModal" />
    <ProtoItemModal @register="registerModal1" @success="reload" />
    <ProtoInfoModal @register="registerModal2" />
    <a-modal title="提示" v-model:open="runVisible" centered @ok="handleRunOk">
      <p class="modal-content">确定{{ authItemInfo.info.enable ? '停用' : '启用' }}该认证吗？</p>
    </a-modal>
    <a-modal title="提示" v-model:open="deleteVisible" centered @ok="handleDeleteOk">
      <p class="modal-content">确定删除该认证吗？</p>
    </a-modal>
  </main>
</template>
<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import ProtocolModal from './ProtocolModal.vue';
  import ProtoItemModal from './ProtoItemModal.vue';
  import ProtoInfoModal from './ProtoInfoModal.vue';
  import { actionBtns, extensions } from './protocol.data';
  import { saveOrUpdate, deleteRequest, infoRequest } from './protocol.api';
  import { useModal } from '/@/components/Modal';
  const [registerModal, { openModal }] = useModal();
  const [registerModal1, { openModal: openItemModal }] = useModal();
  const [registerModal2, { openModal: openInfoModal }] = useModal();
  const authItemInfo = reactive({ info: {} });
  const runVisible = ref<boolean>(false);
  const deleteVisible = ref<boolean>(false);
  const protocolList = [
    {
      protocolType: 'GBT32960',
      host: '0.0.0.0',
      port: 8976,
      enable: true,
      status: true,
    },
    {
      protocolType: 'WEBSOCKET',
      host: '0.0.0.0',
      port: 8083,
      enable: false,
      path: '/{clientId}',
      basicAuth: false,
      status: false,
    },
  ];
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
    });
  }
  function handleRun(record) {
    authItemInfo.info = record;
    runVisible.value = true;
  }
  async function handleRunOk() {
    authItemInfo.info.enable = !authItemInfo.info.enable;
    await saveOrUpdate(authItemInfo.info);
  }
  function handleDelete(record) {
    authItemInfo.info = record;
    deleteVisible.value = true;
  }
  async function handleDeleteOk() {
    await deleteRequest(authItemInfo.info);
  }
  function handleEdit(record) {
    openItemModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }
  function handleDetail(record) {
    openInfoModal(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }
  function headerBtnItemHandle(item, i) {
    headerBtnHandle(item, i.name);
  }
  function headerBtnHandle(item, methodName) {
    const actions = {
      handleRun,
      handleDelete,
      handleEdit,
      handleDetail,
    };
    const action = actions[methodName as keyof typeof actions];
    action && action(item);
  }
</script>
<style lang="less" scoped>
  p {
    margin-bottom: 0;
  }
  .setting-page {
    padding: 20px;
    background-color: #fff;
    box-shadow: 0 0 18px 0 rgba(0, 0, 0, 0.06);
    border-radius: 8px;
  }
  .setting-page .form-title {
    display: flex;
    margin: 16px 0 8px;
  }
  .form-title .title {
    font-size: 15px;
    text-align: left;
    height: 34px;
    line-height: 34px;
    font-weight: 600;
    color: #0b1e42;
  }
  .common-btn {
    background: linear-gradient(144deg, rgba(18, 76, 255, 0.12), rgba(67, 147, 255, 0.12));
    border-radius: 18px;
    font-size: 13px;
    color: #1d6bff;
    margin: 0 16px;
    border: none;
  }
  .setting-page .card-list {
    display: flex;
    flex-wrap: wrap;
  }
  .card-list .card {
    flex: 0 0 390px;
    height: 138px;
    overflow: hidden;
    background: #f5f8fa;
    border-radius: 8px;
    padding: 20px;
    margin: 0 16px 16px 0;
  }
  .card-head {
    display: flex;
    justify-content: space-between;
  }
  .card-head .left {
    flex: 1;
  }
  .card-head .right {
    flex: 0 0 70px;
    text-align: right;
  }
  .card-head .right .status-img {
    height: 20px;
  }
  .left .card-image {
    width: 36px;
    height: 36px;
    border-radius: 4px;
  }
  .left .card-name {
    font-size: 15px;
    font-weight: 600;
    color: #0b1e42;
    margin-top: 8px;
  }
  .card-list .card .card-bottom {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .card-list .card .card-row {
    font-size: 13px;
    color: #788aa0;
    line-height: 18px;
    margin-top: 6px;
  }
  .card-list .card .card-bottom .btns {
    flex: 0 0 240px;
    text-align: right;
  }
  .card-list .card .card-bottom .btns .row-btn-img {
    width: 23px;
    height: 23px;
    margin-left: 8px;
    margin-right: 10px;
    cursor: pointer;
  }
  .modal-content {
    height: 80px;
    line-height: 80px;
    padding-left: 30px;
    font-size: 15px;
  }
</style>
